package com.example.demos.web.controller;

import com.example.demos.dto.ReturnOrderDetailsListDTO;
import com.example.demos.dto.ReturnOrdersBySupplierNameDTO;
import com.example.demos.entity.Orders;
import com.example.demos.entity.ReturnOrders;
import com.example.demos.entity.Store;
import com.example.demos.service.ReturnOrdersService;
import com.example.demos.util.PageBean;
import com.example.demos.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/v2/returnorders")
public class ReturnOrdersController {
    @Autowired
    private ReturnOrdersService returnOrdersService;

    @GetMapping("/selectStore")
    public Result<PageBean<List<Store>>> selectStore() {
        return Result.ok(returnOrdersService.selectStore());
    }

    @GetMapping("/listByPage")
    public Result<PageBean<List<ReturnOrders>>> listByPage(String state, Integer page, Integer rows) {
        return Result.ok(returnOrdersService.select(state, page, rows));
    }

    @PostMapping("/add")
    public Result<Void> add(@RequestBody ReturnOrderDetailsListDTO returnOrderDetailsListDTO, HttpSession session) {
        return returnOrdersService.addReturnOrders(returnOrderDetailsListDTO, session) == -1
                ?
                Result.bizErr("Bad Request")
                : Result.ok("添加成功");
    }

    @PostMapping("/doCheck")
    public Result<Void> doCheck(Integer id, HttpSession session) {
        returnOrdersService.doCheck(id, session);
        return Result.ok("已审核");
    }

    @PostMapping("/doInStore")
    public Result<Void> doInStore(@RequestParam("id") BigInteger returnOrdersDetailUuid, @RequestParam("storeuuid") BigInteger storeUuid, HttpSession session) {
        returnOrdersService.doInStore(returnOrdersDetailUuid, storeUuid, session);
        return Result.ok("出库成功");
    }

    @GetMapping("/salesReturnlistByPage")
    public Result<PageBean<List<ReturnOrdersBySupplierNameDTO>>> salesReturnListByPage(Integer page, Integer rows) {
        return Result.ok(returnOrdersService.selectReturnListByPage(page, rows));
    }

}
